import {queryRoomList,queryRoomU} from '../../services/tRoom';
import {queryRackList,qryById} from '../../services/cabinet/cabinet';
import { create } from '../../services/user';

export default {
    namespace: 'topology',
    state: {
        modalVisible : false,
        newKey: '',
        modalType:'create',
        roomId:'',
        roomList:[],
        rackList:[],
        rackDetailList:[],
        type:'1',
        roomU:[],
    },
    reducers: {     
        hideInfo (state) {
            return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
        },
        upState(state, { payload}) {
            return { ...state, ...payload };
        },
    },
    effects: {
        *queryRoom({ payload },{ call, put }) {
            const { body } = yield call(queryRoomList,payload);
            yield put({ type: 'upState', payload: {roomList:body.data,roomId:body.data[0].id_} });
        },
        *queryRackList({ payload },{ call, put }) {
            const { body } = yield call(queryRackList,payload);
            yield put({ type: 'upState', payload: {rackList:body.data} });
        },
        *searchRackList({ payload },{ call, put }) {
            yield put({ type: 'upState', payload: {rackList:[]} });
            const { body } = yield call(queryRackList,payload);
            yield put({ type: 'upState', payload: {rackList:body.data,roomId:payload.roomId} });
        },
        *showInfo({ payload },{ call, put }) {       
            const { body } = yield call(qryById,payload);
            yield put({ type: 'upState', payload: {rackDetailList:body.data, modalType:'edit', newKey : new Date().getTime() + '' , modalVisible: true} });
        },
        *getRoomU({ payload },{ call, put }) {       
            const { body } = yield call(queryRoomU,payload);
            yield put({ type: 'upState', payload:{roomU:body.data}});
        },
        *changeRoomId({ payload },{ call, put }) {
            yield put({ type: 'upState', payload: {roomId:payload}});
        },       
        *typeChange({ payload },{ call, put }) {   
            yield put({ type: 'upState', payload});
            yield put({ type: 'queryRackList', payload});
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname }) => {
                if (pathname === '/topology') {
                    dispatch({ type: 'upState', payload:{type:'1' ,rackList:[]} });    
                    $("#topology tr td").css("background-color","rgb(255,255,255)");
                    $("#topology tr td").text("");
                    $("#topology tr td").removeAttr("key") ;    
                    dispatch({ type: 'queryRackList', payload:{ 'roomId':'',type:'1' } });                    
                    dispatch({ type: 'getRoomU', payload:{ 'roomId':'' }});
                    dispatch({ type: 'queryRoom' });
                   
                }
            });
        },
    },
}